<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

include_once("impl/ImageImpl.php");
include_once("impl/TagImpl.php");

include_once("utils/FileUtils.php");
include_once("utils/ImageUtils.php");

	
$imgImpl 	= new ImageImpl(DB_TAG_MM);
$tagImpl	= new TagImpl(DB_TAG_MM);

$fileUtils	= new FileUtils();											//--> Files


$error_message 	= '';

$name		= '';
$summary	= '';
$status		= '1';

$imgDto = $imgImpl->getById($id);

if(isset($_POST['Submit']) && is_object($imgDto)){
	$name			= $_POST['name'];
	$level			= $_POST['level'];
	$description	= $_POST['description'];
	$status			= $_POST['status'];
	$tags			= $_POST['tags'];
	
	$arrImages = array();

	$blacklist = array(".php", ".phtml", ".php3", ".php4");
	
	if(empty($_FILES["image"])){
		$error_message = "please select upload image.";
	}else{
		foreach ($_FILES["image"]["error"] as $key => $error) {
	    	if ($error == UPLOAD_ERR_OK) {
				foreach ($blacklist as $item) {
					if(preg_match("/$item\$/i", $_FILES['image']['name'][$key])) {
					 	$error_message = "only support gif and jpg format.";
					}
				}
				
				if(empty($error_message)){
					$imageinfo = getimagesize($_FILES['image']['tmp_name'][$key]); 
		
					if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { 
						$error_message = "only support gif and jpg format. Your upload image format: " . $imageinfo['mime'];
					}else if($file_size >4098000)
						$error_message = 'Your image more than 4098 kb('.ceil($file_size/1024).'kb)';
					else{
						/*-- get ext image file name --*/
						$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
						$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
						
						$arrImage = array();
						$arrImage['name'] 		= $_FILES['image']['name'][$key];
						$arrImage['tmp_name'] 	= $_FILES['image']['tmp_name'][$key];
						$arrImage['file_size']	= filesize($_FILES['image']['tmp_name'][$key]);
						
						$arrImage['width']		= $imageinfo[0];
						$arrImage['height']		= $imageinfo[1];
						
						$arrImage['md5']		= md5(file_get_contents($_FILES['image']['tmp_name'][$key]));
						$arrImage['extName']	= $image_extend_name;
						
						$arrImages[] 			= $arrImage;
					}
				}		
	    	}
	    	
	    	if(!empty($error_message)){
	    		break;
	    	}
		}
	}
	
	foreach ($arrImages as $arrImg) {				
		$oldImgPath = $fileUtils->getFullPathByID(IMAGE_PATH_CONTENT, $imgDto->id, $imgDto->id . '.' . strtolower($imgDto->extName));
		if(file_exists($oldImgPath))
			unlink($oldImgPath);
			
		$newImgPath = $fileUtils->getFullPathByID(IMAGE_PATH_CONTENT, $imgDto->id, $imgDto->id . '.' . strtolower($arrImg['extName']));
		if (move_uploaded_file($arrImg['tmp_name'], $newImgPath)) {//-- save image
			$error_message = "edit image success.";
		}else
			$error_message = "edit image failure.";					
		
		$imgDto->width			= $arrImg['width'];
		$imgDto->height			= $arrImg['height'];
		$imgDto->extName		= $arrImg['extName'];
		$imgDto->fileSize		= $arrImg['file_size'];
		$imgDto->md5			= $arrImg['md5'];
		$imgDto->isAnimated		= ImageUtils::isAnimatedGif($arrImg['tmp_name']);
		
		$imgDto->level 			= $level;
		$imgDto->name 			= $name;
		$imgDto->description	= $description;
		$imgDto->status			= $status;
	
		$isOk = $imgImpl->edit($imgDto);
		
		if($isOk){
			$tagIds = $tagImpl->addTags($tags);
			foreach($tagIds as $tagId)
				$imgImpl->addTag($imgDto->id, $tagId);
			
			$error_message .= " ". $imgDto->id . " edit image succeeded.";
		}else
			$error_message .= " ". $imgDto->id . " edit image failure.";			
	}
	
	if(empty($arrImages)){
		$imgDto->level 			= $level;
		$imgDto->name 			= $name;
		$imgDto->description	= $description;
		$imgDto->status			= $status;
	
		$isOk = $imgImpl->edit($imgDto);
		
		if($isOk){
			$imgImpl->delTags($imgDto->id);
			$tagIds = $tagImpl->addTags($tags);
			foreach($tagIds as $tagId)
				$imgImpl->addTag($imgDto->id, $tagId);
			
			$error_message .= " ". $imgDto->id . " edit image succeeded.";
		}else
			$error_message .= " ". $imgDto->id . " edit image failure.";
	}
		

	echo "<script language='javascript'>";
	echo "alert(\"$error_message\");";
	echo "location.href=\"$backurl\";";
	echo "</script>";
}

$tags 	= $imgImpl->getTags($imgDto->id);
$imgDto->tags = $tags;

$arrStatus = array('1' => 'Online', '0' => 'Offline');


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;

$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('status', $status);

$smarty->assign('id', $id);
$smarty->assign('imgDto', $imgDto);

$smarty->display('listPic_Edit.htm');
?>
